package com.golden.procedure.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.golden.procedure.domain.QueryRequest;
import com.golden.procedure.domain.SingleRequest;
import com.golden.procedure.domain.SiteTemplateDO;
import com.golden.procedure.domain.SiteTemplateDetailDO;
import com.golden.procedure.query.SiteTemplateQueryDO;
import com.golden.procedure.result.PageResponse;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.SingleResponse;
import com.golden.procedure.service.ISiteTemplateService;
import com.golden.procedure.util.EncryptedUtil;
import com.golden.procedure.util.HttpStatus;
import com.google.gson.Gson;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 摄控模板管理 <b>Description:</b><br>
 * 
 * @author Reading.Reagan</a>
 * @version 1.0
 * @Note <b>ProjectName:</b> procedure <br>
 *       <b>PackageName:</b> com.golden.procedure.controller <br>
 *       <b>ClassName:</b> SiteTemplateController <br>
 *       <b>Date:</b> 2018年10月22日 下午6:17:16
 */
@RestController
@RequestMapping(value = "/api/template")
public class SiteTemplateController extends BaseController {

	@Autowired
	ISiteTemplateService siteTemplateService;

	public static String encodeObject(SingleResponse<SiteTemplateDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodePage(PageResponse<SiteTemplateDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodeDetailPage(PageResponse<SiteTemplateDetailDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	@ApiOperation(value = "查询模板列表", notes = "上门组查模板")
	@ApiImplicitParam(name = "siteID", value = "上门组ID", required = true, dataType = "Integer")
	@RequestMapping(value = { "/query.json" }, method = RequestMethod.POST)
	public String query(@RequestBody QueryRequest<SiteTemplateQueryDO> request) {

		// {"data":
		// {"siteID":1},"version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		SiteTemplateQueryDO query = request.getData();

		PageResponse<SiteTemplateDO> response = new PageResponse<SiteTemplateDO>();
		ResultDO resultDO = siteTemplateService.query(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<SiteTemplateDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "查询模板详情列表", notes = "")
	@ApiImplicitParam(name = "id", value = "模板ID", required = true, dataType = "Integer")
	@RequestMapping(value = { "/queryDetail.json" }, method = RequestMethod.POST)
	public String queryDetail(@RequestBody QueryRequest<SiteTemplateQueryDO> request) {

		// {"data": {"siteID":1, "srcTemplateID":5,
		// "templateName":"copy模板0928"},
		// "version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		SiteTemplateQueryDO query = request.getData();
		if (query.getPageRows() > 0) {
			query.setPage(true);
		}

		PageResponse<SiteTemplateDetailDO> response = new PageResponse<SiteTemplateDetailDO>();
		ResultDO resultDO = siteTemplateService.queryDetail(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<SiteTemplateDetailDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
			if (query.isPage()) {
				response.setTotal((Integer) resultDO.getModel(ResultDO.SECOND_MODEL_KEY));
			}
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeDetailPage(response, PLANINTEXT);
	}

	@ApiOperation(value = "保存模板", notes = "保存新模板，更新原有模板")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "上门组ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "srcTemplateID", value = "数据源模板ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "destTemplateID", value = "待更新模板ID(更新时)", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "templateName", value = "新模板名称(新增时)", required = false, dataType = "String") })
	@RequestMapping(value = { "/save.json" }, method = RequestMethod.POST)
	public String saveFromTemplate(@RequestBody SingleRequest<SiteTemplateDO> request) {

		// {"data": {"siteID":1, "srcTemplateID":5,
		// "templateName":"copy模板0928"},
		// "version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		SiteTemplateDO templateDO = request.getData();
		templateDO.setAuthorID(9);
		templateDO.setAuthor("李玉玲");

		SingleResponse<SiteTemplateDO> response = new SingleResponse<SiteTemplateDO>();
		ResultDO resultDO = siteTemplateService.addFromTemplate(templateDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "新增seat时间", notes = "模板新增seat时间")
	@ApiImplicitParam(name = "id", value = "1", required = true, dataType = "Integer")
	@ApiImplicitParams({ @ApiImplicitParam(name = "templateID", value = "模板ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "seatGroupIndex", value = "工位分组序号", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "planStartOn", value = "时段", required = true, dataType = "String") })
	@RequestMapping(value = "/addSeat.json", method = RequestMethod.POST)
	public String addSeat(@RequestBody SingleRequest<SiteTemplateDetailDO> request) {

		// {"data": {"templateID":6, "seatGroupIndex":1,
		// "planStartOnStr":"11:00"},
		// "version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		SiteTemplateDetailDO detailDO = request.getData();
		detailDO.setAuthorID(9);
		detailDO.setAuthor("李玉玲");

		SingleResponse<SiteTemplateDO> response = new SingleResponse<SiteTemplateDO>();

		ResultDO resultDO = siteTemplateService.addSeat(detailDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "编辑seat时间", notes = "模板编辑seat时间")
	@ApiImplicitParam(name = "id", value = "1", required = true, dataType = "Integer")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "席位ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "planStartOnStr", value = "时段", required = true, dataType = "String") })
	@RequestMapping(value = "/editSeat.json", method = RequestMethod.POST)
	public String edit(@RequestBody SingleRequest<SiteTemplateDetailDO> request) {

		// {"data": {"id":180, "planStartOnStr":"11:10"},
		// "version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		SiteTemplateDetailDO detailDO = request.getData();
		detailDO.setModifiID(9);
		detailDO.setModifi("李玉玲");

		SingleResponse<SiteTemplateDO> response = new SingleResponse<SiteTemplateDO>();
		ResultDO resultDO = siteTemplateService.editSeat(detailDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "启用/禁用seat时间", notes = "启用/禁用seat时间")
	@ApiImplicitParam(name = "id", value = "1", required = true, dataType = "Integer")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "status", value = "状态：0启用/-1禁用", required = true, dataType = "Integer") })
	@RequestMapping(value = "/seatStatus.json", method = RequestMethod.POST)
	public String seatStatus(@RequestBody SingleRequest<SiteTemplateDetailDO> request) {

		// {"data": {"id":180, "status":-1},
		// "version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		SiteTemplateDetailDO detailDO = request.getData();
		detailDO.setModifiID(9);
		detailDO.setModifi("李玉玲");

		SingleResponse<SiteTemplateDO> response = new SingleResponse<SiteTemplateDO>();
		ResultDO resultDO = siteTemplateService.seatStatus(detailDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "删除seat", notes = "删除seat时间")
	@ApiImplicitParam(name = "id", value = "席位ID", required = true, dataType = "Integer")
	@RequestMapping(value = "/deleteSeat.json", method = RequestMethod.POST)
	public String deleteSeat(@RequestBody SingleRequest<SiteTemplateDetailDO> request) {

		SiteTemplateDetailDO detailDO = request.getData();
		// http://localhost:9988/api/schSetting/seatStatus.json
		// {"data":
		// {"siteID":1,"schedulingDateStart":"1535760000000","schedulingDateEnd":"1536537600000","siteSeatID":2382,"planStartOnStr":"11:00","status":0},"version":1,"token":"W57m
		// iUG3/D y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}

		SingleResponse<SiteTemplateDO> response = new SingleResponse<SiteTemplateDO>();
		ResultDO resultDO = siteTemplateService.deleteSeat(detailDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "删除组", notes = "模板管理：删除组")
	@ApiImplicitParam(name = "id", value = "1", required = true, dataType = "Integer")
	@ApiImplicitParams({ @ApiImplicitParam(name = "templateID", value = "模板ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "seatGroupIndexs", value = "工位分组序号[1,2]", required = true, dataType = "Integer[]") })
	@RequestMapping(value = "/deleteSeatGroup.json", method = RequestMethod.POST)
	public String deleteSeatGroup(@RequestBody SingleRequest<SiteTemplateDetailDO> request) {

		// {"data": {"templateID":6,
		// "seatGroupIndexs":[4,5]},"version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		SiteTemplateDetailDO detailDO = request.getData();
		detailDO.setModifiID(9);
		detailDO.setModifi("李玉玲");

		SingleResponse<SiteTemplateDO> response = new SingleResponse<SiteTemplateDO>();
		ResultDO resultDO = siteTemplateService.delete(detailDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

}
